#include "GameController.h"
#include "TranslateBoard.h"

GameController::GameController(QWidget *parent) {
    gameView = new GameView(parent);
    chessBoardController = new ChessBoardController(gameView);
    chessPanelRecController = new ChessPanelRecController(gameView);
    gameHistory = new GamesHistory();

    simulation = new Simulation(chessBoardController, chessPanelRecController);

    connect(chessPanelRecController, SIGNAL(currentRecord(int)), this, SLOT(setCurrentGame(int)));
    connect(chessPanelRecController, SIGNAL(stop()), this, SLOT(reset()));
}

void GameController::start() {
    gameView->show();

    //edit
    chessBoardController->initBoard();
}

void GameController::loadGamesFromFile(const QString &fileName) {

    try {
        FileHandler handler(fileName);
        QString cont = handler.read();
        Scanner scanner(&cont);

        if (scanner.scan()){
            QVector<QString> * tokens = scanner.getTokens();
            Parser parser(tokens);

            if (parser.parse()){
                QVector<QString> * parTokens = parser.getTokens();
                Generator generator(gameHistory);
                generator.generate(parTokens);

                TranslateBoard a(gameHistory);

                a.translate();
            }
        }

        foreach(GameRecord * gameRecord, *(gameHistory->getGames())) {
            chessPanelRecController->addGameInfo(gameRecord->getGameInfo());
        }

        /*****************************************/
//        int i, j;
//        for (i = 0; i < gameHistory->getGames()->count(); i++) {
//            qDebug() << "Gra" << i;
//            for (j = 0; j < gameHistory->getGames()->at(i)->getGameMoves()->getMoves()->count(); j++) {
//                QString a = gameHistory->getGames()->at(i)->getGameMoves()->getMoves()->at(j)->getName();
//                QString b = gameHistory->getGames()->at(i)->getGameMoves()->getMoves()->at(j)->getSquareEnd();
//                if (a.right(1) == "+") {a = a.left(a.length()-1);}
//                if (a.right(2) != b) {
//                    qDebug() << a << "-" << b;
//                    qDebug() << "  " << gameHistory->getGames()->at(i)->getGameMoves()->getMoves()->at(j)->getCastlingBegin() << gameHistory->getGames()->at(i)->getGameMoves()->getMoves()->at(j)->getCastlingEnd();
//                }
//            }
//        }
        /*****************************************/

       // simulation->setGameRecord(gameHistory->getGames()->at(0));
    }catch(FileException & e) {
        e.show();
    }
}

void GameController::setCurrentGame(int index)
{
    simulation->setGameRecord(gameHistory->getGames()->at(index));
}

void GameController::reset()
{
   simulation->wait(1);
   if(simulation->isRunning()) {
       simulation->terminate();
   }

    chessBoardController->resetBoard();
    delete simulation;
    simulation = new Simulation(chessBoardController, chessPanelRecController);
    simulation->setGameRecord(gameHistory->getGames()->at(0));
}

GameController::~GameController() {
    delete gameHistory;
    delete simulation;
    delete chessBoardController;
    delete chessPanelRecController;
}
